﻿using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BJoin.DapperExtension.DatabaseFactory
{
	public static class DbFactoryExtensions
	{
		public static async Task<TReturn> Using<TReturn, TDatabase>(this IFactory<TDatabase> me, Func<TDatabase, Task<TReturn>> handler)
			where TDatabase : IDisposable
		{
			using var dataProvider = me.Create();
			return await handler(dataProvider);
		}

		public static async Task Using<TDatabase>(this IFactory<TDatabase> me, Func<TDatabase, Task> handler)
			where TDatabase : IDisposable
		{
			using (var dataProvider = me.Create())
			{
				await handler.Invoke(dataProvider);
			}
		}

		//
		//public static async Task<IEnumerable<T>> GetPageAsync<T>(this IDatabase db, Query<T> query, IList<ISort> sort = null, int? commandTimeout = null) where T : class
		//{
		//	var pageResult = await db.GetPageAsync<T>(query.Page ?? 0, query.Take ?? 10, query, sort, commandTimeout);
		//	return pageResult;
		//}

		//public static async Task<T> FindOneAsync<T>(this IDatabase db, object predicate = null, IList<ISort> sort = null, int? commandTimeout = null) where T : class
		//{
		//	var entities = await db.GetPageAsync<T>(0, 1, predicate, sort, commandTimeout);
		//	return entities.FirstOrDefault();
		//}

	}
}
